/*****************************************************************************
 * dev_relay.c
 *
 * Copyright (C) 2020 Jeasonvor 1101627719@qq.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 ****************************************************************************/

/*****************************************************************************
 * Included Files
 ****************************************************************************/
#include "plum_api.h"
#include "thirdparty/modbus/modbus.h"
/*****************************************************************************
 * Trace Definitions
 ****************************************************************************/
#define LOG_RUN_LEVEL LOG_LEVEL_DEBUG
#define LOG_MODULE    "dev.relay"
#include "thirdparty/log/log.h"
/*****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

/*****************************************************************************
 * Private Types
 ****************************************************************************/

/*****************************************************************************
 * Private Function Prototypes
 ****************************************************************************/

typedef struct {
    plum_u8  idx;
    plum_u16 addr;
    plum_u16 valve;
} modbus_relay_addr_sheet_t;
/*****************************************************************************
 * Private Data
 ****************************************************************************/

static modbus_relay_addr_sheet_t dev_relay_tab[] = {
    {1, 0x0000, 0}, {2, 0x0001, 0}, {3, 0x0002, 0}, {4, 0x0003, 0},
    {5, 0x0004, 0}, {6, 0x0005, 0}, {7, 0x0006, 0}, {8, 0x0007, 0},
};
/*****************************************************************************
 * Public Data
 ****************************************************************************/

/*****************************************************************************
 * Private Functions
 ****************************************************************************/

PLUM_PRIVATE
plum_u16 dev_relay_get_addr(plum_u8 idx)
{
    plum_u8 loop;
    for (loop = 0; loop < PLUM_ARRAY_CNT(dev_relay_tab); loop++) {
        if (dev_relay_tab[loop].idx == idx) {
            return (dev_relay_tab[loop].addr);
        }
    }

    return 0xFFFF;
}

plum_void dev_relay_notice_handle(modbus_protocol_evt evt, plum_void *arg)
{
    switch (evt) {
        case MODBUS_EVT_MSG:
        {
            modbus_msg_t *sta = (modbus_msg_t *)arg;
            plum_u8       loop;
            for (loop = 0; loop < PLUM_ARRAY_CNT(dev_relay_tab); loop++) {
                if (dev_relay_tab[loop].addr == sta->addr) {
                    dev_relay_tab[loop].valve = *(plum_u8 *)sta->value;
                    break;
                }
            }
        } break;

        case MODBUS_EVT_ERR:
            LOG_E("the read active err");
            break;
    }
}

/*****************************************************************************
 * Public Functions
 ****************************************************************************/

PLUM_PUBLIC
plum_s32 dev_relay_init(plum_void)
{
    plum_s32 rc = PLUM_ECODE_OK;

    return (rc);
}

PLUM_PUBLIC
plum_s32 dev_relay_on(plum_u8 id, plum_u32 idx)
{
    plum_u8 data[] = {0x00, 0xFF};

    return modbus_protocol_write(MODBUS_PROTOCOL_RTU, id,
                                 MODBUS_PROTOCOL_WRITE_SCOIL,
                                 dev_relay_get_addr(idx), data, 2);
}

PLUM_PUBLIC
plum_s32 dev_relay_off(plum_u8 id, plum_u32 idx)
{
    plum_u8 data[] = {0x00, 0x00};

    return modbus_protocol_write(MODBUS_PROTOCOL_RTU, id,
                                 MODBUS_PROTOCOL_WRITE_SCOIL,
                                 dev_relay_get_addr(idx), data, 2);
}

PLUM_PUBLIC
plum_bit dev_relay_read(plum_u8 id, plum_u32 idx)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        rc = modbus_protocol_register_notice(dev_relay_notice_handle);
        if (rc) {
            LOG_E("modbus_protocol_register_notice err,rc: %d", rc);
            break;
        }

        rc = modbus_protocol_read(MODBUS_PROTOCOL_RTU, id,
                                  MODBUS_PROTOCOL_READ_SCOIL,
                                  dev_relay_get_addr(idx), 1);
        if (rc) {
            LOG_E("modbus_protocol_write err,rc: %d");
            break;
        }

        plum_u8 loop;
        for (loop = 0; loop < PLUM_ARRAY_CNT(dev_relay_tab); loop++) {
            if (dev_relay_tab[loop].idx == idx) {
                rc = dev_relay_tab[loop].valve;
                break;
            }
        }
    } while (0);

    if (rc > 0) {
        return (plum_true);
    }

    return (plum_false);
}

/****************************************************************************/
/*                                                                          */
/*  End of file.                                                            */
/*                                                                          */
/****************************************************************************/
